package com.yunxin.core.infra.persistence.mapper.order;

import cn.hutool.core.date.DateTime;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.yunxin.core.application.admin.team.DistributeDo;
import com.yunxin.core.application.admin.team.Team;
import com.yunxin.core.application.command.order.OperateAllocationOrderRest;
import com.yunxin.core.application.dto.order.OrderUserApplyAllocationMerchantPageConnectDTO;
import com.yunxin.core.application.dto.order.OrderUserApplyAllocationMerchantPageDTO;
import com.yunxin.core.domain.model.order.OrderUserApplyAllocation;
import com.yunxin.core.infra.persistence.entity.order.OrderUserApplyAllocationDO;
import com.yunxin.core.infra.persistence.entity.order.OrderUserApplyDetailDO;
import com.yunxin.core.infra.persistence.entity.order.query.condition.OrderUserApplyAllocationMerchantPageCondition;
import com.yunxin.core.infra.persistence.entity.order.query.condition.OrderUserApplyAllocationPageCondition;
import com.yunxin.core.infra.persistence.entity.order.query.result.OrderCountResult;
import com.yunxin.core.infra.persistence.entity.order.query.result.OrderUserApplyAllocationMerchantPageResult;
import com.yunxin.core.infra.persistence.entity.order.query.result.OrderUserApplyAllocationPageResult;
import com.yunxin.core.utils.infra.support.request.PushOrderDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author zero°
 * @since 2023-06-02
 */
@DS("core")
@Mapper
public interface OrderUserApplyAllocationExtendMapper extends OrderUserApplyAllocationMapper {

    /**
     * 根据申请订单号及分配类型获取最新的用户申请分配订单
     * @param applyOrderNo applyOrderNo
     * @param allocationTypes allocationTypes
     * @return OrderUserApplyAllocation
     */
    OrderUserApplyAllocationDO selectByApplyOrderNoAndAllocationTypes(@Param("applyOrderNo") String applyOrderNo,
                                                                      @Param("allocationTypes") List<Integer> allocationTypes);

    /**
     * 分页查询订单列表总条数
     * @param condition condition
     * @return int
     */
    long countOrderUserApplyAllocationPageQuery(OrderUserApplyAllocationPageCondition condition);

    /**
     * 分页查询订单列表
     * @param condition condition
     * @return List<OrderUserApplyAllocationPageResult>
     */
    List<OrderUserApplyAllocationPageResult> orderUserApplyAllocationPageQuery(OrderUserApplyAllocationPageCondition condition);

    /**
     * 分页查询订单列表总条数-商户端
     * @param condition condition
     * @return int
     */
    long countOrderUserApplyAllocationMerchantPageQuery(OrderUserApplyAllocationMerchantPageCondition condition);

    /**
     * 分页查询订单列表-商户端
     * @param condition condition
     * @return List<OrderUserApplyAllocationMerchantPageResult>
     */
    List<OrderUserApplyAllocationMerchantPageResult> orderUserApplyAllocationMerchantPageQuery(OrderUserApplyAllocationMerchantPageCondition condition);

    /**
     * 根据分配订单id更新用户微信号
     * @param id id
     * @param userWechat userWechat
     * @return count
     */
    int updateAllocationOrderWechatById(@Param("id") Long id,
                                        @Param("userWechat") String userWechat);


    /**
     * 根据分配订单id更新用户微信号
     * @param id id
     * @param enterpriseWechat enterpriseWechat
     * @return count
     */
    int updateAllocationOrderEnterpriseWechatById(@Param("id") Long id,
                                        @Param("enterpriseWechat") String enterpriseWechat);

    /**
     * 根据商户id获取分配单最新账户id
     * @param merchantId merchantId
     * @return accountId
     */
    Long getAllocationOrderLastAccountIdByMerchant(@Param("merchantId") Long merchantId,@Param("code")String code);

    /**
     * 根据用户code和商户id查询分配订单id
     * @param userCode userCode
     * @param merchantId merchantId
     * @param startTime startTime
     * @param endTime endTime
     * @return
     */
    Long getAllocationIdByUserAndMerchant(@Param("userCode") String userCode,
                                          @Param("merchantId") Long merchantId,
                                          @Param("startTime") Date startTime,
                                          @Param("endTime") Date endTime);

    /**
     * 根据产品id查询时间范围内分配订单总数
     * @param productId productId
     * @param startTime startTime
     * @param endTime endTime
     * @return count
     */
    Integer getAllocationOrderCountByProductId(@Param("productId") Long productId,
                                          @Param("startTime") Date startTime,
                                          @Param("endTime") Date endTime);

    /**
     * 根据产品id查询当日订单条数
     * @param productIds productIds
     * @param startTime startTime
     * @param endTime endTime
     * @return Result<Boolean>
     */
    List<OrderCountResult> listOrderCountByProductIds(@Param("productIds") Set<Long> productIds,
                                                      @Param("startTime") Date startTime,
                                                      @Param("endTime") Date endTime);

    List<OrderUserApplyDetailDO> getApplyOrderNoByDate(OrderUserApplyAllocationPageCondition commond);

    /**
     * 查询用户信息
     * @param values
     * @return
     */
    List<OrderUserApplyAllocationMerchantPageConnectDTO> findUserDetail(@Param("values") Collection<OrderUserApplyAllocationMerchantPageDTO> values);

    /**
     * 查询当前商户信息
     * @param userId
     * @return
     */
    Long merchantIdByUserId(@Param("userId") Long userId);

    /**
     * 保存任务
     * @param distributeDo
     * @return
     */
    int saveTeamTask(DistributeDo distributeDo);

    /**
     * 查询任务列表
     * @return
     */
    List<DistributeDo> selectTeamTask(@Param("startTime") DateTime startTime,@Param("endTime") DateTime endTime);

    /**
     * 根据订单id查询订单详情
     * @param orderIds
     * @return
     */
    List<OrderUserApplyAllocation> selectOrderDetail(@Param("orderIds") List<Long> orderIds);


    /**
     * 分页查询订单列表-商户用户端
     * @param condition condition
     * @return List<OrderUserApplyAllocationMerchantPageResult>
     */
    List<OrderUserApplyAllocationMerchantPageResult> orderUserApplyAllocationMerchantPageQueryForMerchantUser(@Param("condition") OrderUserApplyAllocationMerchantPageCondition condition, @Param("sysUserId") Long sysUserId);

    /**
     * 校验订单是否被分配
     * @param orderIds
     * @return
     */
    List<Integer> selectalreadyDistribute(@Param("orderIds") List<Long> orderIds);

    /**
     * 未接通列表
     * @param condition
     * @return
     */
    List<OrderUserApplyAllocationMerchantPageResult> orderUserApplyAllocationMerchant(OrderUserApplyAllocationMerchantPageCondition condition);

    /**
     * 分配单列表
     * @param values
     * @return
     */
    List<OrderUserApplyAllocationMerchantPageConnectDTO> findUserDetailForMarchan(@Param("values") Collection<OrderUserApplyAllocationMerchantPageDTO> values);

    /**
     * 分配订单查询团队信息
     * @param dto
     * @return
     */
    List<Team> selectTeamList(@Param("dto") OrderUserApplyAllocationMerchantPageDTO dto);


    OrderUserApplyAllocationDO selectOrderAllocation(@Param("applyOrderNo") String applyOrderNo);

    /**
     * 根据订单号查询用户详情
     * @param customerId
     * @return
     */
    PushOrderDTO selectOrderDetailByApplyOrderNo(@Param("customerId") Integer customerId);

    Integer getAllocationOrderCountByMerchantId(@Param("merchantId")Long merchantId,
                                                @Param("merchantStartTime")Date merchantStartTime,
                                                @Param("merchantEndTime")Date merchantEndTime);
}
